package svn.project;

import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;

import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;

import org.tmatesoft.svn.core.SVNLock;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;



public class SVNConnectManager {
	public SVNConnectManager(String url){
		initSvnManger(url);
	}
	
	public void initSvnManger(String url){
		DAVRepositoryFactory.setup();
		String userName="waen";
		String userPwd="111111";
		String url2=url;//url:  http://192.168.0.12:8081/svn
		try{
			SVNURL svnUrl = SVNURL.parseURIDecoded(url);
			repository = SVNRepositoryFactory.create(svnUrl);
			
			ISVNAuthenticationManager authManager = new BasicAuthenticationManager(userName, userPwd);
			repository.setAuthenticationManager(authManager);
			svnUrl = repository.getLocation();			
		}
		catch(SVNException e){
			System.err.println("Error in init the system!");
		}
	}
	
	public String checkInPath(String url) {
		String exception = "";
		try{
			SVNURL svnUrl = SVNURL.parseURIDecoded(url);
			repository.setLocation(svnUrl, false);
			SVNNodeKind nodeKind= repository.checkPath("", -1);
			if(nodeKind == SVNNodeKind.NONE){
				exception = "NONE";
			}
			else if(nodeKind == SVNNodeKind.FILE){
				exception = "FILE";
			}
			else if(nodeKind == SVNNodeKind.DIR){
				exception = "DIR";
			}
		}
		catch(SVNException svne) {
			exception = "ERROR";
		}
		return exception;
	}

	public SVNLock getLock(String fileLocalPath) {
		System.out.println("fileLocalPath:" + fileLocalPath);
		try {
			System.out.println("repository.getLock(fileLocalPath)"+repository.getLock(fileLocalPath));
			if(repository.getLock(fileLocalPath) != null) {
				return repository.getLock(fileLocalPath);
			}
		}catch(SVNException svne) {
			System.out.println(svne);
		}
		return null;
	}
	
	public SVNRepository getRepository() {
		return repository;
	}
	
	private SVNRepository repository;
}
